package com.loyal.mwm.ui.settings

import android.content.Intent
import android.view.View
import android.widget.AdapterView
import com.base.kit.GsonUtil
import com.loyal.mwm.R
import com.loyal.mwm.adapter.SettingsLinkAdapter
import com.loyal.mwm.base.BaseSwipeActivity
import com.loyal.mwm.beans.LinkBean
import com.loyal.mwm.databinding.ActivitySettingsBinding
import com.loyal.mwm.util.ImageUtil

class SettingsActivity : BaseSwipeActivity<ActivitySettingsBinding>(),
    AdapterView.OnItemClickListener {

    override fun actLayoutRes(): Int {
        return R.layout.activity_settings
    }

    override fun afterOnCreate() {
        val toolbar = binding.toolbar.root
        toolbar.title = "设置"
        setSupportActionBar(toolbar)
        binding.drawable = ImageUtil.getBackground(this)
        val beanList = GsonUtil.json2List(
            this, "json/settings.json", LinkBean::class.java,
            true
        )
        binding.adapter = SettingsLinkAdapter(beanList)
        binding.listView.onItemClickListener = this
    }

    override fun setEdgePosition(): Int {
        return LEFT
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) return
        setResult(resultCode, data)
        finish()
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        val linkBean = parent.adapter.getItem(position) as? LinkBean?
        val className = linkBean?.linkClass
        val forResult = replaceText(linkBean?.forResult)
        try {
            when (forResult) {
                /*"next" -> {
                    hasIntentParams(true)
                    startActivityByAct(className)
                }
                "nextForResult" -> {
                    hasIntentParams(true)
                    startActivityForResultByAct(className, IntImpl.reqCodeSettings_Account)
                }
                "finishSetResult" -> {
                    setResult(Activity.RESULT_OK)
                    finish()
                }*/
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}